package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.StockMainBoard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.StockMainBoardMapper;
import com.ruoyi.system.service.IStockMainBoardService;
import com.ruoyi.common.core.text.Convert;

/**
 * 股票主板Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@Service
public class StockMainBoardServiceImpl implements IStockMainBoardService 
{
    @Autowired
    private StockMainBoardMapper stockMainBoardMapper;

    /**
     * 查询股票主板
     * 
     * @param mainId 股票主板主键
     * @return 股票主板
     */
    @Override
    public StockMainBoard selectStockMainBoardByMainId(Long mainId)
    {
        return stockMainBoardMapper.selectStockMainBoardByMainId(mainId);
    }

    /**
     * 查询股票主板列表
     * 
     * @param stockMainBoard 股票主板
     * @return 股票主板
     */
    @Override
    public List<StockMainBoard> selectStockMainBoardList(StockMainBoard stockMainBoard)
    {
        return stockMainBoardMapper.selectStockMainBoardList(stockMainBoard);
    }

    /**
     * 新增股票主板
     * 
     * @param stockMainBoard 股票主板
     * @return 结果
     */
    @Override
    public int insertStockMainBoard(StockMainBoard stockMainBoard)
    {
        stockMainBoard.setCreateTime(DateUtils.getNowDate());
        return stockMainBoardMapper.insertStockMainBoard(stockMainBoard);
    }

    /**
     * 修改股票主板
     * 
     * @param stockMainBoard 股票主板
     * @return 结果
     */
    @Override
    public int updateStockMainBoard(StockMainBoard stockMainBoard)
    {
        stockMainBoard.setUpdateTime(DateUtils.getNowDate());
        return stockMainBoardMapper.updateStockMainBoard(stockMainBoard);
    }

    /**
     * 批量删除股票主板
     * 
     * @param mainIds 需要删除的股票主板主键
     * @return 结果
     */
    @Override
    public int deleteStockMainBoardByMainIds(String mainIds)
    {
        return stockMainBoardMapper.deleteStockMainBoardByMainIds(Convert.toStrArray(mainIds));
    }

    /**
     * 删除股票主板信息
     * 
     * @param mainId 股票主板主键
     * @return 结果
     */
    @Override
    public int deleteStockMainBoardByMainId(Long mainId)
    {
        return stockMainBoardMapper.deleteStockMainBoardByMainId(mainId);
    }


    @Override
    public String importStockMainBoard(List<StockMainBoard> stockMainBoardList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(stockMainBoardList) || stockMainBoardList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (StockMainBoard stockMainBoard : stockMainBoardList)
        {
            try
            {
                // 验证是否存在这个数据
                StockMainBoard selectMainBoardselect = new StockMainBoard();
                selectMainBoardselect.setStockCode(stockMainBoard.getStockCode());
                List<StockMainBoard> stockMainBoards = stockMainBoardMapper.selectStockMainBoardList(selectMainBoardselect);
                if (stockMainBoards.isEmpty()){
                    stockMainBoard.setCreateBy(operName);
                    stockMainBoard.setCreateTime(DateUtils.getNowDate());
                    int i = stockMainBoardMapper.insertStockMainBoard(stockMainBoard);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、股票 " + stockMainBoard.getStockName() + " 导入成功");
                }else{
                    stockMainBoard.setMainId(stockMainBoards.get(0).getMainId());
                    stockMainBoard.setUpdateBy(operName);
                    stockMainBoard.setUpdateTime(DateUtils.getNowDate());
                    stockMainBoardMapper.updateStockMainBoard(stockMainBoard);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、股票 " + stockMainBoard.getStockName() + " 更新成功");
                }
            }
            catch (Exception e)
            {
//                failureNum++;
//                String loginName = user.getLoginName();
//                if (ExceptionUtil.isCausedBy(e, ConstraintViolationException.class))
//                {
//                    loginName = EscapeUtil.clean(loginName);
//                }
//                String msg = "<br/>" + failureNum + "、账号 " + loginName + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 用户状态修改
     *
     * @param stockMainBoard 用户信息
     * @return 结果
     */
    @Override
    public int changeStatus(StockMainBoard stockMainBoard)
    {
        return stockMainBoardMapper.updateStockMainBoard(stockMainBoard);
    }

}
